package com.youxin.logprocess.bean;

import java.util.Calendar;
import java.util.Date;

import org.apache.hadoop.hdfs.server.namenode.status_jsp;

import com.youxin.foundation.utils.DateUtil;
import com.youxin.logprocess.common.MonitorDateUtils;
import com.youxin.logprocess.common.MonitorRedisKeyGenerator;

/**
 * 统计时间主要用于产生统计需要的一些时间类型
 * @author yingde.cao
 *
 */
public class StatTimeBean {
	private String currentMinute;
	private String twoMinutes;
	private String fiveMinutes;
	private String quarterMinutes;
	private String twentyMinutes;
	private String halfHourMinutes;
	private String hourMinutes;
	
	private Date date;
	
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public String getCurrentMinute() {
		return currentMinute;
	}
	public void setCurrentMinute(String currentMinute) {
		this.currentMinute = currentMinute;
	}
	public String getTwoMinutes() {
		return twoMinutes;
	}
	public void setTwoMinutes(String twoMinutes) {
		this.twoMinutes = twoMinutes;
	}
	public String getFiveMinutes() {
		return fiveMinutes;
	}
	public void setFiveMinutes(String fiveMinutes) {
		this.fiveMinutes = fiveMinutes;
	}
	public String getQuarterMinutes() {
		return quarterMinutes;
	}
	public void setQuarterMinutes(String quarterMinutes) {
		this.quarterMinutes = quarterMinutes;
	}
	public String getTwentyMinutes() {
		return twentyMinutes;
	}
	public void setTwentyMinutes(String twentyMinutes) {
		this.twentyMinutes = twentyMinutes;
	}
	public String getHalfHourMinutes() {
		return halfHourMinutes;
	}
	public void setHalfHourMinutes(String halfHourMinutes) {
		this.halfHourMinutes = halfHourMinutes;
	}
	public String getHourMinutes() {
		return hourMinutes;
	}
	public void setHourMinutes(String hourMinutes) {
		this.hourMinutes = hourMinutes;
	}	
	/**
	 * 生成统计发短信邮件所需要的时间段
	 */
	public void generateStatTimes()
	{
		this.date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		this.currentMinute = DateUtil.getFormatTime(calendar);
		calendar.setTime(date);
		this.twentyMinutes = MonitorDateUtils.getMinuteTwentyFormat(calendar);
		calendar.setTime(date);
		this.hourMinutes = MonitorDateUtils.getMinuteFormat(calendar, 60);		
	}
	/**
	 * 生成Nginx统计所有需要的时间信息
	 */
	public void generateNginxStatTimes()
	{
		this.date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		this.currentMinute = DateUtil.getFormatTime(calendar);
		
		calendar.setTime(date);
		this.twoMinutes = MonitorDateUtils.getMinuteFormat(calendar, 2);
		
		calendar.setTime(date);
		this.fiveMinutes = MonitorDateUtils.getMinuteFormat(calendar, 5);
		
		calendar.setTime(date);
		this.quarterMinutes = MonitorDateUtils.getMinuteFormat(calendar, 15);
		
		calendar.setTime(date);
		this.twentyMinutes = MonitorDateUtils.getMinuteTwentyFormat(calendar);
		
		calendar.setTime(date);
		this.hourMinutes = MonitorDateUtils.getMinuteFormat(calendar, 60);	
	}
	
	
	
	/**
	 * 是否处在当前时间内
	 * @param date 时间
	 * @return
	 */
	private boolean isInMinuteTime(Date compareDate,int offset)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		
		calendar=MonitorDateUtils.getCalendarSegment(calendar, offset);
		Date beginDate=calendar.getTime();	
		calendar.add(Calendar.MINUTE, offset);
		Date endDate=calendar.getTime();
		return DateUtil.between(beginDate, endDate, compareDate);
	}
	
	public boolean isInCurrentMinuteTime(Date compareDate)
	{
		return isInMinuteTime(compareDate, 1);
	}
	
	public boolean isInTwoMinutesTime(Date compareDate)
	{
		return isInMinuteTime(compareDate, 2);
	}
	
	public boolean isInFiveMinutesTime(Date compareDate)
	{
		return isInMinuteTime(compareDate, 5);
	}
	
	public boolean isInTwentyMinutesTime(Date compareDate)
	{
		return isInMinuteTime(compareDate, 20);
	}
	
	public String getTwoMinutesHostTimeKey()
	{
		return "two:ip:"+twoMinutes;
	}
	
	public String getCurrentMinuteHostTimeKey()
	{
		return "one:ip:"+currentMinute;
	}
	
	public String getFiveMinuteHostTimeKey()
	{
		return "five:ip:"+fiveMinutes;
	}	
	
	/**
	 * 保存nginx监控主机的20分钟的key值
	 * @return
	 */
	public String getTwentyMinuteHostTimeKey()
	{
		return "twenty:ip:"+twentyMinutes;
	}
	
	public String getTwoMinutesTimeKey(){
		//return MonitorRedisKeyGenerator.twoKey(twoMinutes);
		return "two:nginx:"+twoMinutes;
	}
	
	
	public String getFiveMinutesTimeKey(){
		return "five:nginx:"+fiveMinutes;
		//return MonitorRedisKeyGenerator.fiveKey(fiveMinutes);
	}
	
	public String getQuarterMinutesTimeKey(){
		return "quarter:nginx:"+quarterMinutes;
		//return MonitorRedisKeyGenerator.quarterKey(quarterMinutes);
	}
	
	public String getCurrentMinuteTimeKey()
	{
		return "one:nginx:"+currentMinute;
		//return MonitorRedisKeyGenerator.minuteKey(currentMinute);
	}
	
	public String getTwentyMinuteTimeKey(){
		//return MonitorRedisKeyGenerator.twentyKey(twentyMinutes);
		return "twenty:nginx:"+twentyMinutes;
	}
	
	public String getHourMinuteTimeKey(){
		return "hour:nginx:"+hourMinutes;
		//return MonitorRedisKeyGenerator.hourKey(hourMinutes);
	}
	
	
}
